package ru.sviridov.sellandbuy.json;

import java.lang.reflect.Type;

import ru.sviridov.sellandbuy.model.Auth;
import ru.sviridov.sellandbuy.model.IAuth;
import ru.sviridov.sellandbuy.tools.Constants;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

public class AuthSerializer implements JsonSerializer<Auth> {
	@Override
	public JsonElement serialize(Auth src, Type typeOfSrc, JsonSerializationContext context) {
		JsonElement rootElement = context.serialize(src);
		if(!rootElement.isJsonObject()) 
			return rootElement;
		
		JsonObject root = rootElement.getAsJsonObject();
		root.addProperty(IAuth.TOKEN_PROPERTY, Constants.TOKEN);
		
		if(src.isAuthRequered()){
			JsonObject auth = new JsonObject();
			String session = null;//src.getSession(); FIXME temporary. problems with session
			if(session==null) {
				auth.addProperty(IAuth.LOGIN, src.getLogin());
				auth.addProperty(IAuth.PASSWORD, src.getPassword());
			} else
				auth.addProperty(IAuth.SESSION, src.getSession());

			root.add(IAuth.AUTH, auth);			
		}		
		
		return root;
	}
}
